package com.item.service;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.util.StringUtil;
import com.item.dao.BookMapper;
import com.item.dao.CategoryMapper;
import com.item.dao.DepartmentMapper;
import com.item.dao.MajorMapper;
import com.item.dao.SchoolMapper;
import com.item.dao.TypeMapper;
import com.item.dao.model.Book;
import com.item.dao.model.BookExample;
import com.item.dao.model.Category;
import com.item.dao.model.CategoryExample;
import com.item.dao.model.Department;
import com.item.dao.model.DepartmentExample;
import com.item.dao.model.Major;
import com.item.dao.model.MajorExample;
import com.item.dao.model.School;
import com.item.dao.model.SchoolExample;
import com.item.dao.model.Type;
import com.item.dao.model.TypeExample;
import com.item.daoEx.BookMapperEx;
import com.item.daoEx.model.BookEx;
import com.item.daoEx.model.Year;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired 
    private DepartmentMapper departmentMapper;
    @Autowired 
    private TypeMapper typeMapper;
    @Autowired 
    private CategoryMapper categoryMapper;
    @Autowired
    private BookMapperEx bookMapperEx;

    private static final Logger logger = Logger.getLogger(BookService.class);

    public int countByExample(BookExample example) {
        return this.bookMapper.countByExample(example);
    }

    public Book selectByPrimaryKey(String id) {
        return this.bookMapper.selectByPrimaryKey(id);
    }

    public List<Book> selectByExample(BookExample example) {
        return this.bookMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.bookMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Book record) {
        return this.bookMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Book record) {
        return this.bookMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(BookExample example) {
        return this.bookMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Book record, BookExample example) {
        return this.bookMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Book record, BookExample example) {
        return this.bookMapper.updateByExample(record, example);
    }

    public int insert(Book record) {
        return this.bookMapper.insert(record);
    }

    public int insertSelective(Book record) {
        return this.bookMapper.insertSelective(record);
    }
    
    public List<BookEx> selectBookByOrder() {
    	return this.bookMapperEx.selectBookByOrder();
    }
    
    public void fillCateInfo(BookEx ex) {
    	for(BookCategory bc : ex.getCtCodeList()) {
    		SchoolExample schoolExample = new SchoolExample();
    		schoolExample.createCriteria().andCodeEqualTo(StringUtil.emptyIfBlank(bc.getSchoolCode()));
    		List<School> sl = schoolMapper.selectByExample(schoolExample);
    		School s = sl.size() > 0 ? sl.get(0) : new School();
    		bc.setSchoolName(s.getName());
    		bc.setSchoolId(s.getId());
    		DepartmentExample departmentExample = new DepartmentExample();
    		departmentExample.createCriteria().andCodeEqualTo(StringUtil.emptyIfBlank(bc.getDepartmentCode())).andPidEqualTo(StringUtil.emptyIfBlank(bc.getSchoolId()));
    		List<Department> dl = departmentMapper.selectByExample(departmentExample); 
    		Department d = dl.size() > 0 ? dl.get(0) : new Department();
    		bc.setDepartmentId(d.getId());
    		bc.setDepartmentName(d.getName());
    		MajorExample majorExample = new MajorExample();
    		majorExample.createCriteria().andCodeEqualTo(StringUtil.emptyIfBlank(bc.getMajorCode())).andPidEqualTo(StringUtil.emptyIfBlank(bc.departmentId));
    		List<Major> ml = majorMapper.selectByExample(majorExample); 
    		Major m = ml.size() > 0 ? ml.get(0) : new Major();
    		bc.setMajorId(m.getId());
    		bc.setMajorName(m.getName());
    		TypeExample typeExample = new TypeExample();
    		typeExample.createCriteria().andCodeEqualTo(StringUtil.emptyIfBlank(bc.getTypeCode()));
    		List<Type> tl = typeMapper.selectByExample(typeExample); 
    		Type t = tl.size() > 0 ? tl.get(0) : new Type();
    		bc.setTypeName(t.getName());
    		bc.setTypeId(t.getId());
    		CategoryExample categoryExample = new CategoryExample();
    		categoryExample.createCriteria().andCodeEqualTo(StringUtil.emptyIfBlank(bc.getCategoryCode())).andPidEqualTo(StringUtil.emptyIfBlank(bc.getTypeId()));
    		List<Category> cl = categoryMapper.selectByExample(categoryExample); 
    		Category c = cl.size() > 0 ? cl.get(0) : new Category();
    		bc.setCategoryId(c.getId());
    		bc.setCategoryName(c.getName());
    		Year y = Year.getYearByCode(bc.getYearCode());
    		bc.setYearId(y.getId());
    		bc.setYearName(y.getName());
    	}
    }
    
    public String getCMCodeForInsert(BookCategory CG) {
		StringBuilder sb = new StringBuilder();
		
		Type type = this.typeMapper.selectByPrimaryKey(CG.getTypeId());
		Category category = this.categoryMapper.selectByPrimaryKey(CG.getCategoryId());
		
		
		String typeCode = "T00";
		String cateCode = "C00";
		
		if(type != null) {
			typeCode = type.getCode();
		}
		
		if(category != null) {
			cateCode = category.getCode();
		}
		
		String[] sIds = CG.getSchoolId().split(",");
		String[] dIds = CG.getDepartmentId().split(",");
		String[] mIds = CG.getMajorId().split(",");
		
		String temp = "";
		for(int i = 0, len = sIds.length; i < len; i++) {
			School school = this.schoolMapper.selectByPrimaryKey(sIds[i]);
			Department department = this.departmentMapper.selectByPrimaryKey(dIds[i]);
			Major major = this.majorMapper.selectByPrimaryKey(mIds[i]);
			sb.append(temp);
			
			if(school != null) {
				sb.append(school.getCode());
			} else {
				sb.append("S00");
			}
			
			if(department != null) {
				sb.append(department.getCode());
			} else {
				sb.append("D00");
			}
			
			if(major != null) {
				sb.append(major.getCode());
			} else {
				sb.append("M00");
			}
			sb.append(typeCode);
			sb.append(cateCode);
			temp = ",";
		}
		

		return sb.toString();
	}
    
    /**
     * 类别封装
     * @author yjh
     *
     */
    public static class BookCategory implements Serializable {
    	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private String schoolId = "";
		private String schoolCode = "";
		private String schoolName = "";
		private String departmentId = "";
    	private String departmentCode = "";
    	private String departmentName = "";
    	private String majorId = "";
    	private String majorCode = "";
    	private String majorName = "";
    	private String typeId = "";
    	private String typeCode = "";
    	private String typeName = "";
    	private String categoryId = "";
    	private String categoryCode = "";
    	private String categoryName = "";
    	private String yearId = "";
    	private String yearCode = "";
    	private String yearName = "";
    	
    	public String getCMCodeForSelect() {
    		return getCMCodeForSelect(this);
    	}
    	
    	/**
    	 * ‘%’为sql通配符
    	 * @param category
    	 * @return
    	 */
    	public String getCMCodeForSelect(BookCategory category) {
    		StringBuilder sb = new StringBuilder();
    		sb.append("%").append(StringUtils.isBlank(category.getSchoolCode()) ? "%" : category.getSchoolCode());
    		sb.append(StringUtils.isBlank(category.getDepartmentCode()) ? "%" : category.getDepartmentCode());
    		sb.append(StringUtils.isBlank(category.getMajorCode()) ? "%" : category.getMajorCode());
    		sb.append(StringUtils.isBlank(category.getTypeCode()) ? "%" : category.getTypeCode());
    		sb.append(StringUtils.isBlank(category.getCategoryCode()) ? "%" : category.getCategoryCode());
    		sb.append(StringUtils.isBlank(category.getYearCode()) ? "%" : category.getYearCode());
    		return sb.toString();
    	}
    	
 
    	
    	
    	
    	public boolean isEmpty() {
    		return StringUtils.isBlank(categoryCode) && StringUtils.isBlank(schoolCode) && StringUtils.isBlank(departmentCode)
    				 && StringUtils.isBlank(majorCode) && StringUtils.isBlank(typeCode) &&  StringUtils.isBlank(yearCode);
    	}
    	
    	public String getYearId() {
			return yearId;
		}

		public void setYearId(String yearId) {
			this.yearId = yearId;
		}

		public String getYearName() {
			return yearName;
		}

		public void setYearName(String yesarName) {
			this.yearName = yesarName;
		}

		public String getYearCode() {
			return yearCode;
		}

		public void setYearCode(String yearCode) {
			this.yearCode = yearCode;
		}

		public String getSchoolCode() {
    		return schoolCode;
    	}

    	public void setSchoolCode(String schoolCode) {
    		this.schoolCode = schoolCode;
    	}

    	public String getDepartmentCode() {
    		return departmentCode;
    	}

    	public void setDepartmentCode(String departmentCode) {
    		this.departmentCode = departmentCode;
    	}

    	public String getMajorCode() {
    		return majorCode;
    	}

    	public void setMajorCode(String majorCode) {
    		this.majorCode = majorCode;
    	}

    	public String getTypeCode() {
    		return typeCode;
    	}

    	public void setTypeCode(String typeCode) {
    		this.typeCode = typeCode;
    	}

    	public String getCategoryCode() {
    		return categoryCode;
    	}

    	public void setCategoryCode(String categoryCode) {
    		this.categoryCode = categoryCode;
    	}

		public String getSchoolId() {
			return schoolId;
		}

		public void setSchoolId(String schoolId) {
			this.schoolId = schoolId;
		}

		public String getSchoolName() {
			return schoolName;
		}

		public void setSchoolName(String schoolName) {
			this.schoolName = schoolName;
		}

		public String getDepartmentId() {
			return departmentId;
		}

		public void setDepartmentId(String departmentId) {
			this.departmentId = departmentId;
		}

		public String getDepartmentName() {
			return departmentName;
		}

		public void setDepartmentName(String departmentName) {
			this.departmentName = departmentName;
		}

		public String getMajorId() {
			return majorId;
		}

		public void setMajorId(String majorId) {
			this.majorId = majorId;
		}

		public String getMajorName() {
			return majorName;
		}

		public void setMajorName(String majorName) {
			this.majorName = majorName;
		}

		public String getTypeId() {
			return typeId;
		}

		public void setTypeId(String typeId) {
			this.typeId = typeId;
		}

		public String getTypeName() {
			return typeName;
		}

		public void setTypeName(String typeName) {
			this.typeName = typeName;
		}

		public String getCategoryId() {
			return categoryId;
		}

		public void setCategoryId(String categoryId) {
			this.categoryId = categoryId;
		}

		public String getCategoryName() {
			return categoryName;
		}

		public void setCategoryName(String categoryName) {
			this.categoryName = categoryName;
		}

		@Override
		public String toString() {
			return "BookCategory [schoolId=" + schoolId + ", schoolCode="
					+ schoolCode + ", schoolName=" + schoolName
					+ ", departmentId=" + departmentId + ", departmentCode="
					+ departmentCode + ", departmentName=" + departmentName
					+ ", majorId=" + majorId + ", majorCode=" + majorCode
					+ ", majorName=" + majorName + ", typeId=" + typeId
					+ ", typeCode=" + typeCode + ", typeName=" + typeName
					+ ", categoryId=" + categoryId + ", categoryCode="
					+ categoryCode + ", categoryName=" + categoryName + "]";
		}
    }
}